//
//  VistorView.swift
//  阿恺微博
//
//  Created by 孙嘉恺 on 16/10/6.
//  Copyright © 2016年 孙嘉恺. All rights reserved.
//

import UIKit

//一定要加NSObjectProtocol

protocol VisitorViewDelegate: NSObjectProtocol {
    func loginClicked ()
}

class VisitorView: UIView {
    
    //weak, 避免循环引用; 定义一个可选的属性
    //弱引用 VisitorView 被RootviewController引用，被设置代理 再去引用Rootviewcontroller，形成循环强引用
    
    weak var delegate: VisitorViewDelegate?
    
    //大图标
    lazy var iconImageView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")
    
    //菊花
    lazy var cirelImageView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    
    //遮罩视图
    lazy var maskImageView: UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    
    //文字label
    lazy var textLabel: UILabel = UILabel(text: "欢迎来到微博首页,快来上我吧", fontSize: 16, alignment: .center)
    
    //注册按钮
    lazy var registButton: UIButton = UIButton(title: "注册", titleColor: globalColor, fontSize: 14,  backgroundImage: "common_button_white_disable", target: self, action: #selector(regist(button:)))
    
    //登录按钮
    lazy var loginButton: UIButton = UIButton(title: "登录", fontSize: 14,  backgroundImage: "common_button_white_disable", target: self, action: #selector(login(button:)))
    
    
    init(){
        super.init(frame: screenBounds)
        
        //UI搭建
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension VisitorView {
    func setupUI() {
        //自动布局的原则: 从上到下,从左到右,依次布局,如果控件较多,每布局完成一个, 就跑一下看看效果
        
        //1. 添加子视图
        addSubview(iconImageView)
        addSubview(cirelImageView)
        addSubview(maskImageView)
        addSubview(textLabel)
        addSubview(registButton)
        addSubview(loginButton)
        
        //2. 设置子视图
        backgroundColor = UIColor(white: 237/255.0, alpha: 1.0)
        
        //3. 自动布局
        iconImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-120)
        }
        
        cirelImageView.snp.makeConstraints { (make) in
            make.center.equalTo(iconImageView)
        }
        
        maskImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(cirelImageView).offset(40)
            make.left.equalTo(self)
            make.right.equalTo(self)
        }
        
        textLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(cirelImageView).offset(180)
            make.left.equalTo(self).offset(50)
            make.right.equalTo(self).offset(-50)
        }
        
        registButton.snp.makeConstraints { (make) in
            make.centerY.equalTo(cirelImageView).offset(280)
            make.left.equalTo(self).offset(80)
            make.size.equalTo(CGSize(width: 90, height: 35))
        }
        
        loginButton.snp.makeConstraints { (make) in
            make.centerY.equalTo(cirelImageView).offset(280)
            make.right.equalTo(self).offset(-80)
            make.size.equalTo(CGSize(width: 90, height: 35))
        }
        
        //添加动画
        addAnimation()
    }
    
    func addAnimation () {
        let amimation = CABasicAnimation(keyPath: "transform.rotation")
        amimation.duration = 10
        amimation.toValue = 2 * M_PI
        amimation.repeatCount = MAXFLOAT
        amimation.isRemovedOnCompletion = false
        
        cirelImageView.layer.add(amimation, forKey: nil)
    }
}

extension VisitorView {
    func login(button: UIButton) {
        print("登录")
        delegate?.loginClicked()

    }
    
    func regist(button: UIButton) {
        print("注册")
    }
}





